﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TableParsers;
using System.ComponentModel;


using SpeakupBackend.Themes;
using SpeakupBackend;

namespace Frontend
{
    /// <summary>
    /// Interaction logic for ThemesControl.xaml
    /// </summary>
    public partial class ThemesControl : UserControl
    {
        public static readonly DependencyProperty CurrentWeekProperty;
        public static readonly DependencyProperty CurrentLevelProperty;

        static ThemesControl()
        {
            FrameworkPropertyMetadata metadata = new FrameworkPropertyMetadata();
            metadata.DefaultValue = Week.GetCurrentWeek();
            CurrentWeekProperty = DependencyProperty.Register("CurrentWeek", typeof(Week), 
                                                       typeof(ThemesControl), metadata);

            metadata = new FrameworkPropertyMetadata();
            metadata.DefaultValue = Level.GetLevel(Level.MinValidLevelNumber);
            CurrentLevelProperty = DependencyProperty.Register("CurrentLevel", typeof(Level), 
                                                        typeof(ThemesControl), metadata);
        }

        public Level CurrentLevel
        {
            get
            {
                return (Level)GetValue(CurrentLevelProperty);
            }
            set
            {
                SetValue(CurrentLevelProperty, value);
            }
        }
        public Week CurrentWeek
        {
            get
            {
                return (Week)GetValue(CurrentWeekProperty);
            }
            set
            {
                SetValue(CurrentWeekProperty, value);
            }
        }

        private BackendContext m_backendCtx = null;
        public BackendContext BackendCtx
        {
            get
            {
                return m_backendCtx;
            }
            set
            {
                m_backendCtx = value;
                foreach (WorkshopThemeGrid grid in Grids)
                {
                    grid.BackendCtx = value;
                }

                GroupThemeGrid.BackendCtx = value;
                ClubThemeGrid.BackendCtx = value;
            }
        }

        WorkshopThemeGrid[] Grids
        {
            get
            {
                return new WorkshopThemeGrid[] 
                { 
                  DigestThemeGrid, 
                  GrammarThemeGrid,
                  ConversationThemeGrid, 
                  WritingThemeGrid, 
                  ReadingThemeGrid
                };
            }
        }

        public ThemesControl()
        {
            InitializeComponent();

            foreach (WorkshopThemeGrid grid in Grids)
            {
                grid.BackendCtx = BackendCtx;
                grid.HighlitedWeekThemeChanged += WeekThemeGrid_HighlitedWeekThemeChanged;
            }
            GroupThemeGrid.BackendCtx = BackendCtx;
            ClubThemeGrid.BackendCtx = BackendCtx;

            //observe changes to CurrentWeek and CurrentLevel
            {
                DependencyPropertyDescriptor currentWeekDescriptor =
                    DependencyPropertyDescriptor.FromProperty(CurrentWeekProperty, typeof(ThemesControl));

                currentWeekDescriptor.AddValueChanged(this, CurrentWeekChanged);

                DependencyPropertyDescriptor currentLevelDescriptor =
                    DependencyPropertyDescriptor.FromProperty(CurrentLevelProperty, typeof(ThemesControl));

                currentLevelDescriptor.AddValueChanged(this, CurrentLevelChanged);
            }

            CurrentWeekValue.Text = CurrentWeek.ToString();
            CurrentLevelValue.Text = CurrentLevel.ToString();
        }

        void CurrentWeekChanged(object sender, EventArgs e)
        {
            CurrentWeekValue.Text = CurrentWeek.ToString();
            RefreshGrids();
        }

        void CurrentLevelChanged(object sender, EventArgs e)
        {
            CurrentLevelValue.Text = CurrentLevel.ToString();
            RefreshGrids();
        }

        void WeekThemeGrid_HighlitedWeekThemeChanged(object sender, EventArgs e)
        {
            WorkshopThemeGrid grid = (WorkshopThemeGrid)sender;
            WorkshopTheme weekTheme = grid.HighlightedWeekTheme;

            if (weekTheme == null)
            {
                HighlightedClassType.Text = "";
                HighlightedWeek.Text = "";
                HighlightedLevel.Text = "";
                HighlightedTheme.Text = "";
            }
            else
            {
                HighlightedClassType.Text = weekTheme.ClassType.ToString();
                HighlightedLevel.Text = String.Format("Level {0}", weekTheme.LevelGroup);
                HighlightedWeek.Text = String.Format("Week {0:00}",weekTheme.Week);
                HighlightedTheme.Text = weekTheme.Name;
            }
        }

        public void RefreshGrids()
        {
            DigestThemeGrid.SetupGrid(HeaderType.Week, HeaderType.ClassType, 
                                                CurrentWeek, CurrentLevel, null);

            GrammarThemeGrid.SetupGrid(HeaderType.Week, HeaderType.Level, 
                                                CurrentWeek, CurrentLevel, 
                                                WorkshopClassType.GetWorkshopClassType(ClassType.Grammar) );

            ConversationThemeGrid.SetupGrid(HeaderType.Week, HeaderType.Level, 
                                                CurrentWeek, CurrentLevel, 
                                                WorkshopClassType.GetWorkshopClassType(ClassType.Conversation) );

            WritingThemeGrid.SetupGrid(HeaderType.Week, HeaderType.Level, 
                                                CurrentWeek, CurrentLevel, 
                                                WorkshopClassType.GetWorkshopClassType(ClassType.Writing));

            ReadingThemeGrid.SetupGrid(HeaderType.Week, HeaderType.Level, 
                                                CurrentWeek, CurrentLevel, 
                                                WorkshopClassType.GetWorkshopClassType(ClassType.Reading));

            GroupThemeGrid.SetupGrid();
            ClubThemeGrid.SetupGrid();
        }
    }
}
